package com.sys.controller;

import com.basemodel.PageResult;
import com.basemodel.Result;
import com.common.ControllerSupport;
import com.exception.ApiException;
import com.sys.model.SysUser;
import com.sys.service.SysUserService;
import com.sys.service.dto.SysUserDTO;
import com.sys.service.dto.SysUserQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author: cxt
 * @time: 2022/7/21
 */
@Slf4j
@RestController
@Api(tags = "接口调用测试")
@RequestMapping("/te")
public class SysUserController extends ControllerSupport {

    private SysUserService sysUserService;

    @GetMapping("/find/page")
    @ApiOperation("分页查询")
    public PageResult<SysUser> findPage(SysUserQuery query) {
        return sysUserService.findPage(query);
    }

    @GetMapping
    @ApiOperation("根据id获取详情")
    public SysUser getSysUser(@RequestParam Integer id) {
        return sysUserService.get(id);
    }

    @PutMapping
    @ApiOperation("更新")
    public Result<String> updateSysUser(@RequestBody @Valid SysUserDTO dto, BindingResult bindingResult) {
        validErrorHandler(bindingResult);
        sysUserService.updateSysUser(dto);
        return Result.success(Result.UPDATE_SU);
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Result<String> deleteSysUser(@RequestParam Integer id) {
        if (!sysUserService.deleteById(id)) {
            throw new ApiException(Result.DELETE_FA);
        }
        return Result.success(Result.DELETE_SU);
    }

    @PostMapping
    @ApiOperation("保存")
    public Result<String> saveSysUser(@RequestBody @Valid SysUserDTO dto, BindingResult bindingResult) {
        validErrorHandler(bindingResult);
        sysUserService.saveSysUser(dto);
        return Result.success(Result.SAVE_SU);
    }

    @Autowired
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }
}
